package com.example.service.Impl;

import com.example.mapper.EmpMapper;
import com.example.pojo.Emp;
import com.example.pojo.PagBean;
import com.example.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PagBean page(Integer page, Integer pageSize,String name, Short gender , LocalDate begin,LocalDate end) {

        /*//调用empMapper的方法查询总数
        Integer count = empMapper.count();

        //sql语句需要起始索引 和每页数量 参数是第几页和每页数量
        Integer start = (page -1) * pageSize;
        List<Emp> list =  empMapper.page(start,pageSize,name,  gender , begin, end);

        //最后返回给前端是PagBean类型
        PagBean pagBean = new PagBean(count,list);

        return pagBean;*/

        //使用PageHelper插件完成

        //1.设置分页参数、
        PageHelper.startPage(page,pageSize);

        //2.执行查询
        List<Emp> list = empMapper.list(name,gender,begin,end);
        //需要转换类型
        Page<Emp> p = (Page<Emp>) list;
        //调用方法得到总数和查询到的数据
        PagBean pagBean = new PagBean(p.getTotal(),p.getResult());

        return pagBean;
    }

    @Override
    public void deletes(ArrayList<Integer> list2) {

        empMapper.deletes(list2);
    }

    @Override
    public void inset(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.inset(emp);
    }

    @Override
    public Emp deleteid(Integer id) {

        return empMapper.deleteid(id);
    }

    @Override
    public void update(Emp emp) {
        //修改时间
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update2(emp);
    }

        @Override
        public Emp login(Emp emp) {
            return empMapper.getByUSernameAndPosseord(emp);
        }
}
